using System;
using System.Threading;

namespace RJH
{
    public delegate void DownloadCompleteHandler(string key, byte[] dataDownloaded);

    /// <summary>
    /// Summary description for DownloadThread.
    /// </summary>
    public class DownloadThread
    {
        public event DownloadCompleteHandler CompleteCallback;
        public event DownloadProgressHandler ProgressCallback;

        private string _key = "";
        public string Key
        {
            get { return _key; }
            set { _key = value; }
        }

        private string _downloadUrl = "";
        public string DownloadUrl
        {
            get { return _downloadUrl; }
            set { _downloadUrl = value; }
        }

        public DownloadThread(string key, string DownloadUrl)
        {
            this.Key = key;
            this.DownloadUrl = DownloadUrl;
        }

        public void Download()
        {
            Thread t = new Thread(new ThreadStart(StartDownload));
            t.Start();
        }

        private void StartDownload()
        {
            if (CompleteCallback != null && DownloadUrl != "" && Key != "")
            {
                WebDownload webDL = new WebDownload();
                byte[] downloadedData = webDL.Download(Key, DownloadUrl, ProgressCallback);
                CompleteCallback(Key, downloadedData);
            }
        }
    }
}
